package com.dx.mes.service.special.service;

import java.sql.Timestamp;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.special.service.IFsppnljudgeService;
import com.dx.mes.facade.special.tx.Fsppnljge.FsppnljgeI;
import com.dx.mes.facade.special.tx.Fsppnljge.FsppnljgeO;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fsppnljudgeService")
public class FsppnljudgeService implements IFsppnljudgeService{
	
	private LogUtils logUtils;
	private static String TX_NAME = T_FSPPNLJGE;

	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	
	public String subMainProc(String evt_no,String strInMsg) {
		AppContext.clear();
		AppContext.setCurrServiceName(FspboxchgService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FspboxchgService.class);
		logUtils.info("[InTrx:" + strInMsg + "]");
		
		Session session = retPrdInfoDao.getCrSession();
		long rtnCode = _NORMAL;
		String outMsg = "";

		FsppnljgeI inTrx  = new FsppnljgeI();
		FsppnljgeO outTrx = new FsppnljgeO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		outTrx.setType_id(TRX_OUT);
	
		try {
			inTrx = JacksonUtil.fromJson(strInMsg, FsppnljgeI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
			case ACTION_FLG_JUDGE:
				prdJudgeFnc(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retPrdInfoDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			outMsg = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + outMsg + "]");
		}
		return outMsg;
	}

	private void errActionFnc(FsppnljgeO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
		return;
	}

	private void prdJudgeFnc(FsppnljgeI inTrx, FsppnljgeO outTrx) {
		// TODO Auto-generated method stub
		Timestamp gEvtTimestamp = Util.getCurrentTimestamp();
		String prd_seq_id = inTrx.getPrd_seq_id();
		Ret_prd_info prd_info = retPrdInfoDao.get(prd_seq_id);
		if( prd_info == null ){
			outTrx.setRtn_code(E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("该片玻璃["+prd_seq_id+"]在系统中不存在，请确认!");
			return;
		}
		if(!prd_info.getPrd_stat().equals(PROC_WAIT) && !prd_info.getPrd_stat().equals(PROC_IN_PROCESS)){
			outTrx.setRtn_code(E_FSPPNLJGE_INVALID_PRD_STATUE+ "");
			outTrx.setRtn_mesg("玻璃 [" + prd_seq_id + "]状态[" + prd_info.getPrd_stat()+"]不符，必须是WAIT或INPR才可以判定，请检查!");
			return;
		}
		prd_info.setPrd_grade(inTrx.getJudge_result());
		prd_info.setEvt_cate("PJGE");
		prd_info.setEvt_usr(inTrx.getEvt_usr());
		prd_info.setEvt_timestamp(gEvtTimestamp);
		prd_info.setInspection_flg(_YES);
		if(inTrx.getJudge_result().trim().equals(_NG)){
			prd_info.setRepair_flg(_YES);
		}else{
			prd_info.setRepair_flg(_NO);
		}
		if(inTrx.getSht_ope_msg() != null){
			prd_info.setSht_ope_msg(inTrx.getSht_ope_msg());
		}else{
			prd_info.setSht_ope_msg(_SPACE);
		}
		retPrdInfoDao.save(prd_info);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_CODE_OK);
	}
}
